<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<script type="text/javascript">

	Ext.onReady(function() {
			Ext.QuickTips.init();

			Ext.namespace('DataEdit');
			var winDataEdit;

			DataEdit.showWindow = function(entity, id) {

				Ext.Ajax.request({
					url: '<?=site_url()?>admin/getCategoryVal',
					params: { entity: entity, id: id},
					scripts: true
				});

			}

			DataEdit.delete = function(entity, id, name) {
				Ext.MessageBox.confirm('<?=$this->db->escape_str($this->lang->line("CONFIRMATION"))?>', '<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_DELETE_THE_ITEM"))?> (' + name + ')?' , function(btn) {

					if(btn == 'yes') {
						Ext.Ajax.request({url: '<?=site_url()?>admin/data_edit_insert_update', params: 'del=1&ENTITY=' + entity + '&ID=' + id, success: function() {
							Ext.getCmp('treeGrid').getRootNode().reload();
							}
						});
					}

				});
			}

			DataEdit.publish = function(entity, id, name, publish) {
				Ext.MessageBox.confirm('<?=$this->db->escape_str($this->lang->line("CONFIRMATION"))?>', ((publish == 1)?('<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_PUBLISH_THE_ITEMS"))?>'):('<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_CANCEL_THE_UNPUBLISHED_DATA"))?>')) + ' (' + name + ')?' , function(btn) {

					if(btn == 'yes') {
						Ext.Ajax.request({url: '<?=site_url()?>admin/data_edit_insert_update', params: 'publish=' + publish + '&ENTITY=' + entity + '&ID=' + id, success: function() {
							Ext.getCmp('treeGrid').getRootNode().reload();
							}
						});
					}

				});
			}

			DataEdit.image = function(par) {

				var tpl = new Ext.XTemplate(
				'<tpl for=".">',
					'<div class="thumb-wrap" id="ID{e2iId}">',
						'<div class="thumb"><img src="<?=MEDIA?>../uploads/{e2iImageNameThumb}" title="{e2iImageNameOriginal}"></div>',
						'<div class="alt">{e2iAlt}</div>',
					'</div>',
				'</tpl>',
				'<div class="x-clear"></div>'
				);

				var store = new Ext.data.JsonStore({
					url: '<?=site_url()?>admin/get_ent2image/' + par[0] + '/' + par[1],
					root: 'images',
					fields: [
						'e2iId',
						'e2iCouId',
						'e2iCityId',
						'e2iInsId',
						'e2iImageName',
						'e2iImageNameOriginal',
						'e2iImageNameThumb',
						'e2iAlt'
					]
				});
				store.load();

				winImage = new Ext.Window({
					title:'<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>',
					bodyStyle: 'background-color:white',
					autoScroll: true,
					maximized: false,
					modal: true,
					width:700,
					floating: true,
					minWidth: 700,
					height:500,
					minHeight: 500,
					closeAction:'close',
					maximizable: false,

					items:[{

								border: false,

								items: [

									new Ext.Panel({
										id:'images-view',
										frame:false,
										border: false,
										anchor: '100%',
										autoHeight:true,
										collapsible:true,
										layout:'fit',
										title:'<?=$this->db->escape_str($this->lang->line("UPLOADED_IMAGES"))?>',

										tbar: {
											items: [{
													icon: '<?=MEDIA?>icon/application_form_delete.png',
													text: '<?=$this->db->escape_str($this->lang->line("DELETE"))?>',
													ref: '../removeBtn',
													id: 'removeBtn',
													disabled: true,
													handler: function(){
														Ext.MessageBox.confirm('<?=$this->db->escape_str($this->lang->line("CONFIRMATION"))?>', '<?=$this->db->escape_str($this->lang->line("ARE_YOU_SURE_YOU_WOULD_LIKE_TO_DELETE_THE_IMAGE"))?> ?' , function(btn) {
															if(btn == 'yes') {
																var remove = '';
																for(var i = 0; i < Ext.getCmp('thumbDataView').getSelectedRecords().length; i++) {
																	remove += ((remove != '')?('|'):('')) + Ext.getCmp('thumbDataView').getSelectedRecords()[i].data.e2iId;
																}
																Ext.Ajax.request({url: '<?=site_url()?>admin/ent2image_insert_update', params: 'del=1&e2iId=' + remove, success: function() {
																	store.reload();
																	}
																});
															}
														});
													}
												}, {
													icon: '<?=MEDIA?>icon/application_form_add.png',
													text: '<?=$this->db->escape_str($this->lang->line("ADD_EDIT_TEXT"))?>',
													ref: '../addBtn',
													id: 'addBtn',
													disabled: true,
													handler: function(){

														Ext.MessageBox.show({
															title: '<?=$this->db->escape_str($this->lang->line("ADD_EDIT_TEXT"))?>',
															msg: '<?=$this->db->escape_str($this->lang->line("TEXT"))?>:',
															width:300,
															buttons: Ext.MessageBox.OKCANCEL,
															multiline: true,
															fn: function(btn, text) {
																if(btn == 'ok') {
																	var add = '';
																	for(var i = 0; i < Ext.getCmp('thumbDataView').getSelectedRecords().length; i++) {
																		add += ((add != '')?('|'):('')) + Ext.getCmp('thumbDataView').getSelectedRecords()[i].data.e2iId;
																	}
																	Ext.Ajax.request({url: '<?=site_url()?>admin/ent2image_insert_update', params: 'e2iId=' + add + '&e2i_alt=' + text, success: function() {
																		store.reload();
																		}
																	});
																}
															}
													 });

													}
												}
											]
										},

										items: new Ext.DataView({
											store: store,
											autoHeight:true,
											tpl: tpl,
											id: 'thumbDataView',
											multiSelect: true,
											overClass:'x-view-over',
											itemSelector:'div.thumb-wrap',
											//emptyText: '<?=$this->db->escape_str($this->lang->line("UPLOADED_IMAGE_CAN_NOT_BE_FOUND"))?>',

											listeners: {
												selectionchange: {
													fn: function(dv,nodes){

														Ext.getCmp('removeBtn').setDisabled(nodes.length < 1);
														Ext.getCmp('addBtn').setDisabled(nodes.length != 1);

													}
												}
											}

										})
									}),
/*
									new Ext.Panel({
										id:'images-view',
										padding: 10,
										frame:false,
										autoHeight:true,
										collapsible:true,
										layout:'fit',
										anchor: '100%',
										title:'<?=$this->db->escape_str($this->lang->line("UPLOADED_IMAGES"))?>',

										items: new Ext.DataView({
											store: store,
											tpl: tpl,
											autoHeight:true,
											overClass:'x-view-over',
											emptyText: 'No images to display',

											listeners: {
												selectionchange: {
													fn: function(dv,nodes){
														var l = nodes.length;
														var s = l != 1 ? 's' : '';
														panel.setTitle('Simple DataView ('+l+' item'+s+' selected)');
													}
												}
											}
										})
									}),
*/
									new Ext.FormPanel({
										padding: 10,
										labelAlign: 'right',
										labelWidth: 100,
										autoHeight: true,
										autoWidth: true,
										border: false,
										defaultType: 'textfield',
										waitMsgTarget: true,
										items: [{
											xtype:'fieldset',
											title: '<?=$this->db->escape_str($this->lang->line("UPLOADING_IMAGES"))?>',
											autoHeight:true,
											layout: {
												type: 'anchor'
											},
											items: [new AwesomeUploader({
												awesomeUploaderRoot: '<?=MEDIA?>awesomeuploader/',
												uploadUrl: '<?=site_url()?>admin/file_upload',
												flashUploadUrl: '<?=site_url()?>admin/file_upload',
												extraPostData: {ENTITY: par[0], ID: par[1]},
												frame: true,
												disableFlash: true,
												gridHeight: 130,
												anchor: '100%',
												height:200,
												listeners: {
													fileupload: function() {
														store.reload();
													}
												}
												})
											]
										}]

								})
						]

					}],

					bbar: {
						items: [{
							text: '<?=$this->db->escape_str($this->lang->line("CLOSE"))?>',
							handler: function() {
								winImage.close();
							}
						}],
						buttonAlign : 'right'
					}

				}).show();
			}

/*
			DataEdit.publish = function(entity, id, name, publish) {
new Ext.Window({
	iconCls: (rowIndex == 'new')?('application_form_add'):('application_form_edit'),
	bodyStyle: 'background-color:white',
	autoScroll: true,
	maximized: false,
	modal: true,
	width:700,
	floating: true,
	minWidth: 700,
	height:500,
	minHeight: 500,
	closeAction:'close',
	maximizable: false,
	title: (rowIndex == 'new')?('<?=$this->db->escape_str($this->lang->line("ADD_NEW_ARTICLE"))?>'):('<?=$this->db->escape_str($this->lang->line("EDITING_THE_ARTICLE"))?> - ' + record.data.artName)

})*/

			DataEdit.createGridButton = function(cls, text, par, id, name) {
				new Ext.Button(  {
					iconCls: cls,
					tooltip: text + ((name)?(' - ' + name):('')),
					tooltipType: 'qtip',
					renderTo: id,
					//style: 'margin:2px 0 2px 0;',
					handler: function() {
						if(cls == "application_form_preview") {
							window.open(par);
						} else if(cls == 'application_form_delete') {
							DataEdit.delete(par[0], par[1], name);
						} else if(cls == 'application_form_publish') {
							DataEdit.publish(par[0], par[1], name, 1);
						} else if(cls == 'application_form_publish_delete') {
							DataEdit.publish(par[0], par[1], name, 0);
						} else if(cls == 'application_image') {
							DataEdit.image(par);
						} else {
							DataEdit.showWindow(par[0], ((cls == 'application_form_edit')?(''):('-')) + par[1]);
						}
					},
					scope : this
				});
			}

			var tree = new Ext.ux.tree.TreeGrid({

				id: 'treeGrid',
				border: false,
				autoScroll: true,
				listeners: {
					afterrender: function() {
						Ext.getCmp('treeGrid').setWidth(Ext.getCmp('treeGrid').getWidth());
						Ext.getCmp('treeGrid').getEl().child('div[class=x-grid3-header]').setStyle('display', 'none');
					}
				},

				renderTo: 'grid-table-data-edit',
				dataUrl: '<?=site_url()?>admin/getContinentList',

				stripeRows: true,
				autoExpandColumn: 'name',

				columns:[{
						dataIndex: 'name',
						id: 'name',
						width:400
				},
				{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk) {
									var par = v.split('_');
									switch(par[0]) {

										case 'CON': {

											var contentId = Ext.id();
											par[0] = 'COU';
											DataEdit.createGridButton.defer( 1, this, ['application_form_add', '<?=$this->db->escape_str($this->lang->line("ADD_NEW_COUNTRY"))?>', par, contentId]);
											return '<div id="' + contentId + '"></div>'; 

										} break;

										case 'COU': {

											var contentId = Ext.id();
											par[0] = 'CITY';
											DataEdit.createGridButton.defer( 1, this, ['application_form_add', '<?=$this->db->escape_str($this->lang->line("ADD_NEW_COUNTRY"))?>', par, contentId]);
											return '<div id="' + contentId + '"></div>'; 

										} break;

										case 'CITY': {

											var contentId = Ext.id();
											par[0] = 'INS';
											DataEdit.createGridButton.defer( 1, this, ['application_form_add', '<?=$this->db->escape_str($this->lang->line("ADD_NEW_INSTITUTE"))?>', par, contentId]);
											return '<div id="' + contentId + '"></div>'; 

										} break;

										case 'INS': {

											var contentId = Ext.id();
											par[0] = 'MAJ';
											DataEdit.createGridButton.defer( 1, this, ['application_form_add', '<?=$this->db->escape_str($this->lang->line("ADD_NEW_COURSE"))?>', par, contentId]);
											return '<div id="' + contentId + '"></div>'; 

										} break;

										default: {
											return '';
										}

									}
								} else {
									return '';
								}
							}
						})
				},
				{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk) {
									var par = v.split('_');

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_form_edit', '<?=$this->db->escape_str($this->lang->line("EDIT"))?>', par, contentId, record.name]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}

							}
						})
				},
				{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk) {
									var par = v.split('_');

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_form_delete', '<?=$this->db->escape_str($this->lang->line("DELETE"))?>', par, contentId, record.name]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}
							}
						})
				},{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(v.split('_')[0] == 'COU' || v.split('_')[0] == 'CITY' || v.split('_')[0] == 'INS') {
									var par = v.split('_');

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_image', '<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>', par, contentId]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}
							}
						})
				},{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk && record.temp) {
									var par = record.url;

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_form_preview', '<?=$this->db->escape_str($this->lang->line("PREVIEW"))?>', par, contentId, record.name]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}
							}
						})
				},{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk && record.temp) {
									var par = v.split('_');

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_form_publish', '<?=$this->db->escape_str($this->lang->line("PUBLICATION"))?>', par, contentId, record.name]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}
							}
						})
				},{
						width: 30,
						dataIndex: 'entity',
						tpl: new Ext.XTemplate('{entity:this.renderer}', {
							renderer: function(v, record) {
								if(record.szerk && record.temp) {
									var par = v.split('_');

									var contentId = Ext.id();
									DataEdit.createGridButton.defer( 1, this, ['application_form_publish_delete', '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA"))?>', par, contentId, record.name]);
									return '<div id="' + contentId + '"></div>'; 
								} else {
									return '';
								}
							}
						})
				}]

			});
	});

</script>

<div id="grid-table-data-edit"></div>